package com.example.labelreadersample;

import java.util.ArrayList;

public class Gloable {
	
	public static  ArrayList<Card> cardList = null;
	public static  ArrayList<Card> cardListData = null;
	
	public static  int L0 = -31;
	public static int L = -49;
	public static double P = 0.6;
	
	public static cn.wch.ch34xuartdriver.CH34xUARTDriver uartDriver;
	
	
	public static int calDistancse(int rssi[], int rssiCnt)
	{
		int distance, rssiMax, rssiMin, rssiAvr, i;
		
		distance = 0;
		rssiMax = -127;
		rssiMin = 0;
		rssiAvr = 0;
		
		for(i = 0; i < rssiCnt; i++)
		{
			if(rssiMax < rssi[i])
			{
			    rssiMax = rssi[i];
			}
			
			if(rssiMin > rssi[i])
			{
				rssiMin = rssi[i];
			}
			
			rssiAvr += rssi[i];
		}
		
		if(rssiCnt > 3)
		{
		   rssiAvr -= rssiMin;
		   rssiAvr -= rssiMax;
		   
		   rssiAvr = rssiAvr / (rssiCnt - 2);
		}
		else
		{
			rssiAvr = rssiAvr / rssiCnt;
		}
		
		

		if(rssiAvr >= L)
		{
			if(rssiAvr >= L0)
			{
				distance = 0;
			}
			else
			{
				distance = 100  * (L0 - rssiAvr)/(L0 - L);
			}
		}
		else
		{
			distance = 100 + (new Double(((rssiAvr - L) * (rssiAvr - L)) * P)).intValue();
		}
		
		return distance;
		
	}

	public static int crc16(byte[] data, int len)
	{
		int crc, i, j, b;
		
		if(len == 0)
		{
			return 0;
		}
		
		if(data == null)
		{
			return 0;
		}
		
		crc = 0xFFFF;
		
	    for(i = 0; i < len; i++)
	    {
	    	b = data[i] < 0 ? (data[i] + 256) : data[i];
	        crc = crc ^ b;

	        for (j = 0; j < 8; j++)
	        {         
	            if ((crc & 0x01) != 0)
	            {
	                crc = (crc >> 1) ^ 0xA001;
	            }
	            else
	            {
	                crc = crc >> 1;
	            }
	        }
	    }
	    
		return crc;
	}

}
